package servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import model.Notification;
import model.User;
import model.Wish;
import ejb.Facade;

/**
 * Servlet implementation class IndexServlet
 */
@WebServlet("/IndexServlet")
public class IndexServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * Default constructor.
	 */
	public IndexServlet() {
	}

	/**
	 * Constructeur avec parametre
	 * 
	 * @param request
	 * @param response
	 * @throws ServletException
	 * @throws IOException
	 */
	public IndexServlet(HttpServletRequest request, HttpServletResponse response)
			throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	@Override
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		doPost(request, response);
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		User user = (User) request.getSession().getAttribute("user");
		Facade fac = (Facade) request.getSession().getAttribute("Facade");
		//Redirection
		if( user == null ){
			List<User> listLastUser = fac.getLastUser();
			request.getSession().setAttribute("lastUser", listLastUser);
			request.getRequestDispatcher("jsp/index.jsp").forward(request, response);
		}
		else{
			//Recuperer les envies
			List<Wish> listWish = user.getWishes();
			//Mettre en session les envies de l'utilisateur
			request.getSession().setAttribute("listWish", listWish);
			//Mettre en session les notifications de l'utilisateur
			List<Notification> liste = user.getNotification();
			List<String> listeNotif = fac.getListeNotification( liste );
			request.getSession().setAttribute("listNotification", listeNotif);
			//Redirection
			request.getRequestDispatcher("jsp/wallWish.jsp").forward(request, response);
		}
	}

}
